package com.ds.dsm.view.config.nav;

import com.ds.common.JDSException;
import com.ds.config.ListResultModel;
import com.ds.dsm.view.config.form.FormEventView;
import com.ds.enums.db.MethodChinaName;
import com.ds.esd.custom.annotation.CustomAnnotation;
import com.ds.esd.custom.annotation.GridViewAnnotation;
import com.ds.esd.custom.api.APIEvent;
import com.ds.esd.custom.api.annotation.APIEventAnnotation;
import com.ds.esd.custom.bean.MethodConfig;
import com.ds.esd.custom.bean.nav.NavBaseViewBean;
import com.ds.esd.custom.buttonviews.annotation.ButtonViewsAnnotation;
import com.ds.esd.custom.form.enums.CustomFormEvent;
import com.ds.esd.custom.form.enums.CustomFormMenu;
import com.ds.esd.custom.module.annotation.ModuleAnnotation;
import com.ds.esd.dsm.DSMFactory;
import com.ds.esd.dsm.view.ViewEntityConfig;
import com.ds.esd.tool.ui.component.tab.SideBarStatusType;
import com.ds.esd.tool.ui.enums.BarLocationType;
import com.ds.web.util.PageUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Set;

@RequestMapping("/dsm/view/config/nav/")
@Controller
@ButtonViewsAnnotation(barLocation = BarLocationType.top, sideBarStatus = SideBarStatusType.expand, barSize = "3em")
@MethodChinaName(cname = "表单配置")
public class NavActionGroup {


    @CustomAnnotation(hidden = true, pid = true)
    String viewInstId;
    @CustomAnnotation(hidden = true, pid = true)
    String domainId;

    @CustomAnnotation(hidden = true, pid = true)
    String sourceClassName;

    @CustomAnnotation(hidden = true, uid = true)
    String methodName;


    public NavActionGroup() {

    }

    @RequestMapping(method = RequestMethod.POST, value = "NavCustomMenus")
    @GridViewAnnotation(
            addPath = "dsm.view.config.nav.NavMenuTree")
    @ModuleAnnotation( caption = "工具栏",
            imageClass = "spafont spa-icon-c-toolbar")
    @APIEventAnnotation(autoRun = true)
    @ResponseBody
    public ListResultModel<List<NavMenuView>> getNavCustomMenus(String sourceClassName, String methodName,String domainaId, String viewInstId) {
        ListResultModel<List<NavMenuView>> resultModel = new ListResultModel<>();
        try {
            ViewEntityConfig tableConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName,viewInstId);
            MethodConfig methodAPIBean = tableConfig.getSourceConfig().getMethodByName(methodName);
            NavBaseViewBean formViewBean = (NavBaseViewBean) methodAPIBean.getView();
            Set<CustomFormMenu> customFormMenus = formViewBean.getCustomMenu();
            resultModel = PageUtil.getDefaultPageList(customFormMenus, NavMenuView.class);
        } catch (JDSException e) {
            e.printStackTrace();
        }

        return resultModel;
    }


    @MethodChinaName(cname = "按钮组")
    @RequestMapping(method = RequestMethod.POST, value = "NavCustomBottombar")
    @GridViewAnnotation(
            addPath = "dsm.view.config.nav.NavBottomTree")
    @ModuleAnnotation( caption = "按钮组",
            imageClass = "spafont spa-icon-c-statusbutton")
    @APIEventAnnotation(autoRun = true)
    @ResponseBody
    public ListResultModel<List<NavMenuView>> getNavCustomBottombar(String sourceClassName, String methodName,String domainaId, String viewInstId) {
        ListResultModel<List<NavMenuView>> resultModel = new ListResultModel<>();
        try {
            ViewEntityConfig tableConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName, viewInstId);
            MethodConfig methodAPIBean = tableConfig.getSourceConfig().getMethodByName(methodName);
            NavBaseViewBean formViewBean = (NavBaseViewBean) methodAPIBean.getView();
            Set<CustomFormMenu> customFormMenus = formViewBean.getBottombarMenu();
            resultModel = PageUtil.getDefaultPageList(customFormMenus, NavMenuView.class);
        } catch (JDSException e) {
            e.printStackTrace();
        }

        return resultModel;
    }

    @MethodChinaName(cname = "事件")
    @RequestMapping(method = RequestMethod.POST, value = "NavCustomFormEvents")
    @GridViewAnnotation()
    @ModuleAnnotation( caption = "事件", imageClass = "spafont spa-icon-event")
    @APIEventAnnotation(autoRun = true)
    @ResponseBody
    public ListResultModel<List<FormEventView>> getNavCustomFormEvents(String sourceClassName, String methodName,String domainaId, String viewInstId) {
        ListResultModel<List<FormEventView>> resultModel = new ListResultModel<>();
        try {
            ViewEntityConfig tableConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName, viewInstId);
            MethodConfig methodAPIBean = tableConfig.getSourceConfig().getMethodByName(methodName);
            NavBaseViewBean formViewBean = (NavBaseViewBean) methodAPIBean.getView();
            Set<CustomFormEvent> events = formViewBean.getEvent();
            resultModel = PageUtil.getDefaultPageList(events, FormEventView.class);
        } catch (JDSException e) {
            e.printStackTrace();
        }


        return resultModel;
    }

    public String getDomainId() {
        return domainId;
    }

    public void setDomainId(String domainId) {
        this.domainId = domainId;
    }

    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    public String getViewInstId() {
        return viewInstId;
    }

    public void setViewInstId(String viewInstId) {
        this.viewInstId = viewInstId;
    }

    public String getSourceClassName() {
        return sourceClassName;
    }

    public void setSourceClassName(String sourceClassName) {
        this.sourceClassName = sourceClassName;
    }
}
